package LibDL.eval.classification;

import LibDL.core.Dtype;
import LibDL.core.Scalar;
import LibDL.core.Tensor;
import LibDL.core.functional;

public class AccuracyEvaluator<T> extends AbstractClassificationEvaluator<T> {
    private boolean normalize;

    @Override
    protected double core(Tensor truth, Tensor pred) {
        TargetType targetType = checkTarget(truth, pred);
        checkConsistentLength(truth, pred, getSampleWeight());

        Tensor sampleScore;
        if (targetType == TargetType.MULTILABLE_INDICATOR){
            sampleScore = functional.equal_indices(functional.equal_indices(truth, pred).to(Dtype.FLOAT64).sum(1), new Scalar(truth.size(1))).to(Dtype.FLOAT64);
        } else {
            sampleScore = functional.equal_indices(truth,pred).to(Dtype.FLOAT64);
        }
        return weightedSum(sampleScore, getSampleWeight(), isNormalize(),-1).item().to_double();
    }

    public boolean isNormalize() {
        return normalize;
    }

    public AccuracyEvaluator<T> setNormalize(boolean normalize) {
        this.normalize = normalize;
        return this;
    }
}
